package com.zygh.interactiveplatform.module.service;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.interactiveplatform.common.constant.EmployeeResponseCodeConst;
import com.zygh.interactiveplatform.common.domain.ResponseDTO;
import com.zygh.interactiveplatform.module.entity.User;
import com.zygh.interactiveplatform.module.entity.vo.LoginDetailVO;
import com.zygh.interactiveplatform.module.entity.vo.UserPasswordVo;
import com.zygh.interactiveplatform.module.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lee
 * @since 2021-01-28
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    @Resource
    private UserMapper userMapper;


    public ResponseDTO<User> findUserByUsername(String username) {
        User userByUsername = userMapper.findUserByUsername(username);
        if (userByUsername == null) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.EMPTY_INFORMATION_ERROR);
        }
        return ResponseDTO.succData(userByUsername);
    }


    public LoginDetailVO getUserInfo(String username) {
        return userMapper.getUserInfo(username);
    }


    public ResponseDTO addUser(User user) {

        User hasName = userMapper.findUserByUsername(user.getUserName());
        if (hasName != null) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.LOGIN_NAME_EXISTS);
        }
        //默认密码
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //String password = "888888";
        user.setPassword(passwordEncoder.encode(user.getPassword()));

        int res = userMapper.insert(user);
        if (res > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.USER_REGISTER);
    }


    public ResponseDTO updatePwd(UserPasswordVo params) {
        if (StrUtil.isEmpty(String.valueOf(params.getId()))) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.ID_EXIST);
        }

        if (StrUtil.isEmpty(params.getOldPwd())) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.OLDPASSWORD_ERROR);
        }

        if (StrUtil.isEmpty(params.getNewPwd())) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.NEWPASSWORD_ERROR);
        }
        User user = userMapper.selectById(params.getId());
        if (user == null) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.ID_EXIST);
        }
        String password = user.getPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        if (!passwordEncoder.matches(params.getOldPwd(), password)) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.PASSWORD_ERROR);
        }
        user.setPassword(passwordEncoder.encode(params.getNewPwd()));
        userMapper.updateById(user);
        return ResponseDTO.succ();
    }


    public ResponseDTO findUserAll(Integer current, Integer size) {
        PageHelper.startPage(current, size);
        List<User> users = userMapper.selectList(null);
        if (users != null) {
            PageInfo<User> pageInfo = new PageInfo<>(users);
            return ResponseDTO.succData(pageInfo);
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.EMPTY_INFORMATION_ERROR);
    }


    public ResponseDTO updatUserById(User user) {
        //默认密码
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //String password = "888888";
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        int i = userMapper.updateById(user);
        if (i > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.UPDATE_FAILED);
    }


    public ResponseDTO deletUserById(Integer id) {
        int i = userMapper.deleteById(id);
        if (i > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.DELETE_FAILED);
    }
}
